package com.shangyueshang.model;

import com.baomidou.mybatisplus.annotation.TableField;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.shangyueshang.enums.GoodsType;
import com.shangyueshang.serializer.DeserializerBigDecimal;
import com.shangyueshang.serializer.SerializerBigDecimal;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.ToString;

import java.io.Serializable;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.NumberFormat;
import java.time.LocalDate;
import java.time.YearMonth;
import java.util.Optional;

/**
 * @author: JokeyZheng
 * @email: zhengjingfeng@ruqimobility.com
 * @created: 2022/1/17 18:52
 * @version: v1.0.0
 * <p>商品销售统计表</p>
 */
@Data
@ToString(callSuper = true)
@EqualsAndHashCode(callSuper = true)
public class GoodsRevenue extends BaseModel implements BaseShopSkuKey, Serializable {

    /**
     * 店铺名称
     */
    private String shop;

    /**
     * 自有SKU
     */
    private String sku;

    /**
     * 产品名称
     */
    private String name;

    /**
     * 品类
     * @see GoodsType
     */
    private String goodsType;

    /**
     * 统计日期
     */
    private LocalDate tradeDate;

    /**
     * 交易月份
     */
    @TableField(exist = false)
    private YearMonth tradeMonth;

    /**
     * 产品销售总额
     */
    @JsonSerialize(using = SerializerBigDecimal.class)
    @JsonDeserialize(using = DeserializerBigDecimal.class)
    private BigDecimal productSales;

    /**
     * 销售数量
     */
    private Integer saleQuantity;

    /**
     * 销售毛收入，单位：元
     * 已剔除佣金，FBA配送费、税费等
     */
    @JsonSerialize(using = SerializerBigDecimal.class)
    @JsonDeserialize(using = DeserializerBigDecimal.class)
    private BigDecimal saleRevenue;

    /**
     * 佣金费用，单位：元
     */
    @JsonSerialize(using = SerializerBigDecimal.class)
    @JsonDeserialize(using = DeserializerBigDecimal.class)
    private BigDecimal sellingFee;

    /**
     * FBA配送费，单位：元
     */
    @JsonSerialize(using = SerializerBigDecimal.class)
    @JsonDeserialize(using = DeserializerBigDecimal.class)
    private BigDecimal fbaFee;

    /**
     * 头程费用
     */
    @JsonSerialize(using = SerializerBigDecimal.class)
    @JsonDeserialize(using = DeserializerBigDecimal.class)
    private BigDecimal headwayFee;

    /**
     * 采购费用，单位：元
     */
    private BigDecimal purchaseFee;

    /**
     * 包装费用，单位：元
     */
    private BigDecimal packFee;

    /**
     * 退款费用，单位：元
     */
    @JsonSerialize(using = SerializerBigDecimal.class)
    @JsonDeserialize(using = DeserializerBigDecimal.class)
    private BigDecimal returnFee;

    /**
     * 退款数量
     */
    private Integer returnQuantity;

    /**
     * 仓储费，单位：元
     */
    @TableField(exist = false)
    @JsonSerialize(using = SerializerBigDecimal.class)
    @JsonDeserialize(using = DeserializerBigDecimal.class)
    private BigDecimal lastMonthStorageFee;

    /**
     * 仓储费
     *
     * @return
     */
    public BigDecimal getStorageFee() {
        return Optional.ofNullable(lastMonthStorageFee).orElse(BigDecimal.ZERO);
    }

    /**
     * 退货率
     *
     * @return
     */
    public String getReturnRatio() {
        if (this.saleQuantity == 0) {
            return "100.00%";
        }
        if (this.returnQuantity == 0) {
            return "0.00%";
        }
        NumberFormat instance = NumberFormat.getInstance();
        // 设置精确到小数点后2位
        instance.setMaximumFractionDigits(2);

        return instance.format((float) this.returnQuantity / (float) this.saleQuantity * 100) + "%";
    }

    /**
     * FBA费用占比
     *
     * @return
     */
    public String getFbaProportion() {
        if (BigDecimal.ZERO.compareTo(this.productSales) == 0) {
            return "0.00%";
        }
        if (BigDecimal.ZERO.compareTo(this.fbaFee) == 0) {
            return "0.00%";
        }
        return this.fbaFee
                .abs()
                .multiply(BigDecimal.valueOf(100L))
                .divide(productSales, 2, RoundingMode.HALF_UP)
                .toString()
                + "%";
    }

    /**
     * 毛利润
     *
     * @return
     */
    public BigDecimal getNetProfit() {
        return saleRevenue.add(returnFee)
                .add(purchaseFee)
                .add(packFee)
                .add(getAdvertiseFee())
                .add(getStorageFee())
                .add(headwayFee);
    }

    @TableField(exist = false)
    private BigDecimal advertiseFee;

    /**
     * 广告费
     *
     * @return
     */
    public BigDecimal getAdvertiseFee() {
        return Optional.ofNullable(advertiseFee).orElse(BigDecimal.ZERO);
    }

    /**
     * 广告费占比
     *
     * @return
     */
    public String getAdvertiseRatio() {
        if (getAdvertiseFee().compareTo(BigDecimal.ZERO) == 0) {
            return "0.00%";
        }
        if (productSales.compareTo(BigDecimal.ZERO) == 0) {
            return "0.00%";
        }
        return getAdvertiseFee().abs().divide(productSales, 2, BigDecimal.ROUND_HALF_UP).multiply(BigDecimal.valueOf(100L)) + "%";
    }

    @Override
    public String shopSkuKey() {
        return this.shop + "-" + this.sku;
    }
}
